<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <!-- 具体页面结构 -->
        <!-- card title -->
        <template #header>
          <el-row>
            <el-col :span="20">
              <span>{{ company.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <!-- 传入空对象,否则默认传入的是$event,会报错 -->
                      <el-dropdown-item
                        @click.native="showAddDepartmentDialog()"
                        >添加子部门</el-dropdown-item
                      >
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        <!-- card body -->
        <el-tree
          :data="departmentData"
          :props="departmentProps"
          :default-expand-all="true"
          @node-click="handleNodeClick"
        >
          <template #default="{ data }">
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 操作 -->
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="showAddDepartmentDialog(data, 1)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          @click.native="showAddDepartmentDialog(data, 2)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="delDepartment(data)"
                          >删除部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 添加子部门对话框 -->
      <AddDepartment
        ref="editDepartmentRef"
        :add-dialog-visible.sync="addDialogVisible"
        :current-data="currentData"
        :flat-department-data="flatDepartmentData"
        @update-departments="getDepartmentsData"
      ></AddDepartment>
    </div>
  </div>
</template>

<script>
import { getDepartmentsDataApi, delDepartmentByIdApi } from '@/api/departments'
import { transListToTree } from '@/utils/index'
import AddDepartment from './modules/addDepartment.vue'
export default {
  components: {
    AddDepartment
  },
  data () {
    return {
      // 部门列表数据
      departmentData: [],
      // 配置部门列表数据对象
      departmentProps: {
        label: 'name',
        children: 'children'
      },
      // 公司信息
      company: {
        name: '',
        manager: ''
      },
      // 控制添加子部门对话框显示与隐藏
      addDialogVisible: false,
      // 当前操作的部门数据
      currentData: {},
      // 平铺的部门列表数据
      flatDepartmentData: []
    }
  },
  created () {
    this.getDepartmentsData()
  },
  methods: {
    // 树形组件的node的点击事件
    handleNodeClick (currentNode) {
      // console.log(currentNode)
    },
    // 获取部门数据
    async getDepartmentsData () {
      const { companyName, depts } = await getDepartmentsDataApi()
      this.company.name = companyName
      // console.log('处理前的数据')
      // console.table(depts)
      // this.departmentData = depts
      this.departmentData = transListToTree(depts)
      this.flatDepartmentData = depts
    },
    // 删除部门
    async delDepartment (data) {
      try {
        await this.$confirm(`确认要删除${data.name}部门吗?`, '删除提示')
        if (data.children && data.children.length > 0) return this.$message.error('该部门下还有子部门,不能删除')
        await delDepartmentByIdApi(data.id)
        this.$message.success('删除成功')
        this.getDepartmentsData()
      } catch (error) {
        console.log(error)
        this.$message.info('取消删除')
      }
    },
    // 点击显示添加子部门对话框
    // type 1代表新增 2代表编辑
    showAddDepartmentDialog (data, type = 1) {
      // console.log(data)
      if (type === 2) {
        this.$refs.editDepartmentRef.getDepartDetail(data.id)
      }
      this.currentData = data
      this.addDialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
